package www.xudan.android_ffmpeg.videoplayer;



import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import www.xudan.android_ffmpeg.videoplayer.widget.BasePlayerView;


public class PlayerManager {

    private PlayerManager() {

    }

    //正在播放视频的videoView 如果没有正在播放的 则可能为null
    private BasePlayerView playingView;

    public void init() {

    }

    /**
     * 创建一个ijkplayer 现在策略为 为每个videoView 都创建一个新的ijk 但是创建新的之前一般来说会销毁上一个，反复创建的开销和释放在初始化的开销基本一致
     * 但以后也可能更改创建策略
     */
    public IMediaPlayer createIjkPlayer() {
        IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "max-buffer-size", 1000 * 1024);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);


        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", IjkMediaPlayer.SDL_FCC_RV32);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "http-detect-range-support", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "min-frames", 100);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1);
        return new IjkMediaPlayer();
    }

    //单例实体对象
    static PlayerManager manager;

    //线程安全单例获取方式
    public static PlayerManager getInstance() {
        if (manager == null) {
            synchronized (PlayerManager.class) {
                if (manager == null) {
                    manager = new PlayerManager();
                }
            }
        }
        return manager;
    }

    /**
      * 用来设置正在播放的播放器 应用层禁止调用
      */
    public void setPlayingView(BasePlayerView playingView) {
        this.playingView = playingView;
    }
    /**
     * 获取正在播放的播放器
     */
    public BasePlayerView getPlayingView() {
        return playingView;
    }
}
